package cn.geminis.crypto.csp.soft.ec;

import cn.geminis.crypto.csp.soft.AbstractSoftKeyPairGenerator;
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.params.ECNamedDomainParameters;

import java.security.SecureRandom;

/**
 * @author Allen
 */
public class EcKeyGenerator extends AbstractSoftKeyPairGenerator {

    public EcKeyGenerator() {
        var name = "P-256";
        var oid = ECNamedCurveTable.getOID(name);
        var ecSpec = ECNamedCurveTable.getByName(name);
        var domainParameters = new ECNamedDomainParameters(oid, ecSpec.getCurve(), ecSpec.getG(), ecSpec.getN());
        var generationParameters = new ECKeyGenerationParameters(domainParameters, new SecureRandom());
        var ecGenerator = new ECKeyPairGenerator();
        ecGenerator.init(generationParameters);
        super.generator = ecGenerator;
    }

    @Override
    public String getObjectIdentifier() {
        return X9ObjectIdentifiers.id_ecPublicKey.toString();
    }
}
